package tech.abing.spider.task.pageprocessor.amac;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.FundStatEntity;
import tech.abing.spider.task.model.amac.FuturesAccountStatEntity;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.UuIdPublic;

/**
 * 解析期货资管产品备案信息
 * @author Ivan qb.huang@wescxx.com   
 * @date 2016-11-5 上午10:42:46 
 * @version V2.0
 */
public class FuturesAccountProcessor implements PageProcessor {
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue,
			List<Proccessable> objectContainer) throws Exception {
		
		JSONObject jsonObj = JSONObject.fromObject(page.getContent());
		
		boolean hasPre = jsonObj.getBoolean("hasPre");
		PageRequest request = page.getRequest();
		if(!hasPre){//第一页
			long totalPages = jsonObj.getLong("totalPages");
			
			long totalCount = jsonObj.getLong("totalCount");
			FuturesAccountStatEntity stat = new FuturesAccountStatEntity();
			stat.setId(UuIdPublic.timeStrongID(5));
			stat.setFuturesAccountCount(String.valueOf(totalCount));
			stat.setCreateTime(new Date());
			objectContainer.add(stat);
			
			if(totalPages > 1){
				for(int i = 2; i <= totalPages; i++){
					
					PageRequest req = context.createPageRequest(request.getUrl(), FuturesAccountProcessor.class);
					
					req.setMethod(request.getMethod());
					req.setPostType(request.getPostType());
					
					req.setHeaders(request.getHeaders());
					
					req.putParams("nd", TaskUtil.getRandomStrByDate());
					req.putParams("page.pageSize", "50");
					req.putParams("page.pageNo", String.valueOf(i));
					
					req.putParams("page.searchFileName", "publicity_web");
					req.putParams("page.sqlKey", "PAGE_QH_PUBLICITY_WEB");
					req.putParams("page.sqlCKey", "SIZE_QH_PUBLICITY_WEB");
					req.putParams("_search", "false");
					req.putParams("page.orderBy", "MPI_CREATE_DATE");
					req.putParams("page.order", "desc");
					
					req.putParams("filter_LIKES_MPI_NAME", "");
					req.putParams("filter_LIKES_AOI_NAME", "");
					req.putParams("filter_LIKES_MPI_PRODUCT_CODE", "");
					req.putParams("filter_GES_MPI_CREATE_DATE", "");
					req.putParams("filter_LES_MPI_CREATE_DATE", "");
					
					/*req.setRequestParams(request.getRequestParams());
					//覆盖之前的请求参数
					Map<String,String> params =  req.getRequestParams();
					params.put("page.pageNo", String.valueOf(i));
					params.put("nd", TaskUtil.getRandomStrByDate());*/
					/*for (String entry : params) {
						if("page.pageNo".equals(entry.getKey())){
							entry.setValue(String.valueOf(i));
						}
						if("nd".equals(entry.getKey())){
							entry.setValue(TaskUtil.getRandomStrByDate());
						}
					}*/
//					System.out.println(String.valueOf(i));
					/*req.putParams("page.pageNo", String.valueOf(i));
					req.putParams("nd", TaskUtil.getRandomStrByDate());*/
					
					queue.add(req);
					
				}
			}
		}
		
		//解析数据
		JSONArray result = jsonObj.getJSONArray("result");
		String pageIndex = (String) request.getParamsByName("page.pageNo");
		if(result != null && result.size() > 0){
			for(int i = 0; i < result.size(); i++){
				JSONObject retObj = result.getJSONObject(i);
				String MPI_ID = retObj.getString("MPI_ID");
				
				//http://ba.amac.org.cn/pages/amacWeb/qh-web-view.html?id=82778
				String url = "http://ba.amac.org.cn/pages/amacWeb/user!search.action";
				PageRequest req = context.createPageRequest(url, FuturesAccountDetailProcessor.class);
				
				req.setMethod(Method.POST);
				req.setPostType(PostType.FORMDATA);
				
				req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
				req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
				req.putHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				req.putHeader("Host", "ba.amac.org.cn");
				req.putHeader("Referer", "http://ba.amac.org.cn/pages/amacWeb/qh-web-view.html?id="+MPI_ID);
				req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
				req.putHeader("X-Requested-With", "XMLHttpRequest");
				
				req.putParams("filter_EQS_MPI_ID", MPI_ID);
				req.putParams("sqlkey", "publicity_web");
				req.putParams("sqlval", "GET_QH_WEB_BY_MPI_ID");
				
				req.getExtParams().put("retObj", retObj);
				req.getExtParams().put("pageIndex", pageIndex);
				
				queue.add(req);
			}
		}
		

	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
